﻿
using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Entities;
using SqlSugar;
using Surging.Core.Common;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Dora.Services.AdminUser.Repository.Repositorys
{
    /// <summary>
    /// 广告表仓储层
    /// </summary>
    public class AdInfoRepository : SqlSugarRepository<AdInfoEntity, int>
    {
        private ISugarUnitOfWork _unitOfWork;
        public AdInfoRepository(ISugarUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<AdInfoEntity>> GetPageList(AdInfoQueryDto input)
        {
            var pagePredict = _unitOfWork.QueryClient.Queryable<AdInfoEntity>()
                .Where(p => p.IsDeleted == 0);
            var totalCount = await pagePredict.CountAsync();
            var pageList = await pagePredict.OrderBy(it => it.Id, OrderByType.Desc)
                .ToPageListAsync(input.PageIndex, input.PageSize);

            var page = new PageDataList<AdInfoEntity>(totalCount, pageList, input.PageIndex, input.PageSize);
            return page;
        }

        /// <summary>
        /// 根据广告位Id和组织Id获取列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<List<AdInfoEntity>> GetListByPositionIdAndOrgIdList(int positionId, List<int> orgIdList)
        {
            if (orgIdList == null || orgIdList.Count == 0)
            {
                return new List<AdInfoEntity>();
            }
            var result = await _unitOfWork.QueryClient.Queryable<AdInfoEntity>()
                .Where(p => p.IsDeleted == 0)
                .Where(p => p.PositionId == positionId)
                .Where(p => orgIdList.Contains(p.OrgId))
                .OrderBy(it => it.OrgId, OrderByType.Desc)
                .OrderBy(it => it.Sort, OrderByType.Desc)
                .ToListAsync();
            return result;
        }


        /// <summary>
        /// 根据广告位Id和组织Id获取列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> BatchCreate(List<AdInfoEntity> list)
        {
            if (list == null || list.Count == 0)
            {
                return 0;
            }
            var result = await _unitOfWork.ModifyClient.Insertable(list)
                .ExecuteCommandAsync();
            return result;
        }


        /// <summary>
        /// 根据广告位Id和组织Id获取列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> BatchUpdate(List<AdInfoEntity> list)
        {
            if (list == null || list.Count == 0)
            {
                return 0;
            }
            var result = await _unitOfWork.ModifyClient.Updateable(list)
                .UpdateColumns(it => new { it.Name, it.ImgUrl, it.LinkType, it.LinkUrl, it.Sort, it.Remark, it.IsShow, it.UpdatedTime, it.UpdatedUserId, it.UpdatedUserName })
                .ExecuteCommandAsync();
            return result;
        }

        /// <summary>
        /// 根据广告位Id和组织Id获取列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> BatchDelete(List<AdInfoEntity> list)
        {
            if (list == null || list.Count == 0)
            {
                return 0;
            }
            foreach (var item in list)
            {
                item.IsDeleted = 1;
            }
            var result = await _unitOfWork.ModifyClient.Updateable(list)
                .UpdateColumns(it => new { it.IsDeleted, it.UpdatedTime, it.UpdatedUserId, it.UpdatedUserName })
                .ExecuteCommandAsync();
            return result;
        }

        /// <summary>
        /// 验证广告位是否存在并返回数量
        /// </summary>
        /// <param name="OrgId">组织机构编号</param>
        /// <returns>返回广告位数量（-1:组织机构ID传递有误）</returns>
        public async Task<int> ExistAdInfoByOrgId(int OrgId)
        {
            if (OrgId <= 0)
            {
                return -1;
            }
            var result = await _unitOfWork.QueryClient.Queryable<AdInfoEntity>()
                .Where(p => p.OrgId == OrgId).ToListAsync();
            return result.Count;
        }

        //public async Task<int> DeleteInfoByOrgId(int OrgId)
        //{
        //    if (OrgId <= 0)
        //    {
        //        return -1;
        //    }
        //    var result = await _unitOfWork.ModifyClient.Updateable<AdInfoEntity>()
        //        .SetColumns(it=>)
        //        .ExecuteCommandAsync();
        //    return result;

        //}
    }
}
